﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Region
namespace backoffice.Data
{
    partial class Region
    {
        public void setRegion(Region r)
        {
            this.RegionID = r.RegionID; 
            this.CountryID = r.CountryID;
            this.Father = r.Father;
            this.Name = r.Name;
            this.Level = r.Level;
        }
    }
}
#endregion

#region dataRegion
namespace backoffice.Data
{
    public class dataRegion
    {
        #region Create
        public static void createRegion(Region r)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                r.RegionID = Guid.NewGuid();

                db.Regions.InsertOnSubmit(r);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Region getRegion(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Regions.FirstOrDefault(r => r.RegionID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Region> getRegions(Expression<Func<Region, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Regions.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeRegion(Region r)
        {
            try
            {
                if (r == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Region res = db.Regions.FirstOrDefault(region => region.RegionID == r.RegionID);
                    if (res == null) return false;
                    else
                    {
                        res.setRegion(r);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteRegion(Region r)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Regions.Contains(r))
                {
                    Region res = db.Regions.FirstOrDefault(region => region.RegionID == r.RegionID);

                    if (res == null) return false;
                    else
                    {
                        // Delete sells
                        IQueryable<Sell> s = db.Sells.Where((Sell sell) => sell.RegionID == res.RegionID);
                        db.Sells.DeleteAllOnSubmit(s);

                        // Delete buys
                        IQueryable<Buy> b = db.Buys.Where((Buy buy) => buy.RegionID == res.RegionID);
                        db.Buys.DeleteAllOnSubmit(b);

                        // Delete coordinates
                        IQueryable<Coordinate> c = db.Coordinates.Where((Coordinate co) => co.RegionID == res.RegionID);
                        db.Coordinates.DeleteAllOnSubmit(c);

                        // Update land
                        IQueryable<Land> lands = db.Lands.Where((Land la) => la.RegionID == res.RegionID);

                        foreach (Land l in lands)
                        {
                            l.RegionID = null;
                        }

                        // Update users
                        IQueryable<User> users = db.Users.Where((User u) => u.RegionID == res.RegionID);

                        foreach (User u in users)
                        {
                            u.RegionID = null;
                        }

                        // Delete region
                        db.Regions.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion